/**
 * 走访数据
 * Created by douxc on 2015/4/22.
 */
define(function (require, exports, module) {
  'use strict';
  module.exports = function (app) {
    app.register.controller('InterviewDataCtrl', ['$scope', '$filter', 'MHService', '$location', '$routeParams', 'Upload',
      function ($scope, $filter, MHService, $location, $routeParams, Upload) {
        if (MHService.cacheData(false, {key: 'map'})) {
          location.reload();
        }
        $scope.building = {
          estateName: $location.search().name,
          marketer: ''
        };
        /**
         * 新建走访数据
         */
        $scope.visitAdd = function () {
          $location.path('/interview/add');
        };
        MHService.getReq({
          url: MHService.URL.visit_query + $routeParams['Id']
        }, function (data) {
          console.log(data);
          if (data['obj']) {
            var _data = data['obj'];
            var tdata = angular.copy(_data);
            tdata.visitDate = new Date(_data.visitDate.time);
            tdata.baseInfotime = new Date(_data.baseInfotime.time);
            tdata.jointtime = new Date(_data.jointtime.time);
            tdata.planfinishtime = new Date(_data.planfinishtime.time);
            tdata.extension = new Date(_data.extension.time);
            if (!tdata.estateName) {
              tdata.estateName = $location.search().name;
            }
            $scope.building = tdata;
          }
        });
        /**
         * 营销负责人
         */
        MHService.postReq({url: MHService.URL.search_marketer}, function (data) {
          $scope.marketers = data['datas'];
          $scope.selected = data['datas'][0];
          //如果已有负责人
          if ($scope.building.marketer) {
            //将负责人改为选中
            angular.forEach(data['datas'], function (obj) {
              if (obj.id === $scope.building.marketer) {
                $scope.selected = obj;
              }
            });
          } else {
            //没有赋默认值
            $scope.building.marketer = $scope.selected['id'];
          }
        });
        /**
         * 提交数据
         * @param data
         */
        $scope.visitData = function (_data) {
          //时间格式化处理
          var data = angular.copy(_data);
          data.estateId = $routeParams['Id'];//楼盘Id
          data.visitDate = $filter('date')(_data.visitDate, 'yyyy-MM-dd');
          data.baseInfotime = $filter('date')(_data.baseInfotime, 'yyyy-MM-dd');
          data.jointtime = $filter('date')(_data.jointtime, 'yyyy-MM-dd');
          data.planfinishtime = $filter('date')(_data.planfinishtime, 'yyyy-MM-dd');
          data.extension = $filter('date')(_data.extension, 'yyyy-MM-dd');
          data.marketer = $scope.selected['id'];
          MHService.postReq({url: MHService.URL.visit_add, _req: data}, function (data) {
            if (data['success']) {
              MHService.Toast('提交成功');
            } else {
              MHService.Toast('操作失败，请刷新重试');
            }
          });
        };
        /**
         * 推广方案
         */
        $scope.fangAn = function ($files) {
          console.log($files);
          if ($files.length > 0) {
            Upload.upload({url: MHService.URL.img_upload, method: 'POST', file: $files[0]}).success(function (res) {
              //文件上传完成
              alert(JSON.stringify(res.data));
              $scope.building.extensionplan = res.data;
            });
          }
        };
        /**
         * 平面图
         * @param $files
         */
        $scope.pingMian = function ($files) {
          console.log($files);
          if ($files.length > 0) {
            Upload.upload({url: MHService.URL.img_upload, method: 'POST', file: $files[0]}).success(function (res) {
              //文件上传完成
              alert(JSON.stringify(res.data));
              $scope.building.plane = res.data;
            });
          }
        };
      }]);
  };
});
